package com.exmaple.lemlin.utils;

import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.sasl.SASLErrorException;

public class ExceptionSmack {


    /**
     * 自定义 约定异常
     */
    private static final int HTTP_1000 = 1000;//已中止
    private static final int HTTP_1001 = 1001;//帐户已禁用
    private static final int HTTP_1002 = 1002;//凭证已过期
    private static final int HTTP_1003 = 1003;//需要加密
    private static final int HTTP_1004 = 1004;//不正确的编码
    private static final int HTTP_1005 = 1005;//无效的授权
    private static final int HTTP_1006 = 1006;//无效的机制
    private static final int HTTP_1007 = 1007;//无效的机制
    private static final int HTTP_1008 = 1008;//机制太弱
    private static final int HTTP_1009 = 1009;//未经授权
    private static final int HTTP_1010 = 1010;//临时授权失败

    public static IMThrowable HandleException(Throwable e) {
        IMThrowable ex = new IMThrowable(e);
        if (e instanceof SASLErrorException) {
            SASLErrorException exception = (SASLErrorException) e;
            switch (exception.getSASLFailure().getSASLError()) {
                case aborted:
                    ex.setMessage("已中止");
                    ex.setCode(HTTP_1000);
                    break;
                case account_disabled:
                    ex.setMessage("帐户已禁用");
                    ex.setCode(HTTP_1001);
                    break;
                case credentials_expired:
                    ex.setMessage("凭证已过期");
                    ex.setCode(HTTP_1002);
                    break;
                case encryption_required:
                    ex.setMessage("需要加密");
                    ex.setCode(HTTP_1003);
                    break;
                case incorrect_encoding:
                    ex.setMessage("不正确的编码");
                    ex.setCode(HTTP_1004);
                    break;
                case invalid_authzid:
                    ex.setMessage("无效的授权");
                    ex.setCode(HTTP_1005);
                    break;
                case invalid_mechanism:
                    ex.setMessage("无效的机制");
                    ex.setCode(HTTP_1006);
                    break;
                case malformed_request:
                    ex.setMessage("错误的请求");
                    ex.setCode(HTTP_1007);
                    break;
                case mechanism_too_weak:
                    ex.setMessage("机制太弱");
                    ex.setCode(HTTP_1008);
                    break;
                case not_authorized:
                    ex.setMessage("未经授权");
                    ex.setCode(HTTP_1009);
                    break;
                case temporary_auth_failure:
                    ex.setMessage("临时授权失败");
                    ex.setCode(HTTP_1010);
                    break;
            }
        } else if (e instanceof SmackException.AlreadyLoggedInException) {

        } else {
            ex.setMessage("未知错误:" + e.getMessage());
            ex.setCode(9999);
        }
        return ex;
    }

    public static class IMThrowable extends Exception {
        private int code;
        private String message;

        IMThrowable(Throwable throwable) {
            super(throwable);
        }

        public int getCode() {
            return code;
        }

        public void setCode(int code) {
            this.code = code;
        }

        @Override
        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }

}
